<script lang="ts" setup name="GoodsInfo">
import GoodsImage from "./components/goods-image.vue";
import GoodsSales from "./components/goods-sales.vue";
import GoodsName from "./components/goods-name.vue";
import GoodsSku from "./components/goods-sku.vue";
import GoodsDetail from "./components/goods-detail.vue";
import GoodsHot from "./components/goods-hot.vue";

import useStore from "@/store";
import { storeToRefs } from "pinia";
import { ref, watchEffect } from "vue";
import { useRoute } from "vue-router";
import Message from "@/components/message";
import { CartItem } from "@/types/cart";

const { goods, cart } = useStore();
const route = useRoute();

goods.getGoodsInfo(route.params.id as string);

watchEffect(() => {
  if (route.fullPath === `/goods/${route.params.id}`) {
    goods.getGoodsInfo(route.params.id as string);
  }
});

const { goodsInfo } = storeToRefs(goods);

// sku规格
let currentSkuId = "";
const changeSku = (skuId: string) => {
  currentSkuId = skuId;
  const sku = goodsInfo.value.skus.find((item) => item.id === skuId);
  if (sku) {
    goodsInfo.value.inventory = sku.inventory; //更新库存
    goodsInfo.value.price = sku.price; //更新价格
    goodsInfo.value.oldPrice = sku.oldPrice; //更新老价格
  }
};

// 购买数量
const count = ref(1);

// 加入购物车
const addCart = async () => {
  if (currentSkuId === "") {
    Message.warning("请选择完整商品规格");
  }
  // 当前需要添加的商品的sku
  const sku = goodsInfo.value.skus.find((item) => item.id === currentSkuId)!;
  const attrsText = sku.specs
    .map((item) => item.name + ":" + item.valueName)
    .join("");
  await cart.addCart({
    // 本地添加
    id: goodsInfo.value.id,
    name: goodsInfo.value.name,
    picture: goodsInfo.value.mainPictures[0],
    price: goodsInfo.value.price,
    count: count.value,
    skuId: currentSkuId,
    attrsText: attrsText,
    selected: true,
    nowPrice: goodsInfo.value.price,
    stock: goodsInfo.value.inventory,
    isEffective: true,
  } as CartItem);
  Message.success("添加成功");

  console.log("addcart");
};
</script>

<template>
  <div class="xtx-goods-page" v-if="goodsInfo.id">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodsInfo.categories[1].id}`">{{
          goodsInfo.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goodsInfo.categories[0].id}`">{{
          goodsInfo.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ goodsInfo.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 左侧 -->
        <div class="media">
          <GoodsImage :images="goodsInfo.mainPictures"></GoodsImage>
          <GoodsSales></GoodsSales>
        </div>
        <!-- 右侧 -->
        <div class="spec">
          <!-- 商品名称 -->
          <GoodsName :goods="goodsInfo"></GoodsName>
          <!-- sku -->
          <GoodsSku @changeSku="changeSku" :goods="goodsInfo" />
          <!-- numbox -->
          <XtxNumbox v-model="count"></XtxNumbox>

          <!-- 加入购物车按钮 -->
          <XtxButton
            type="primary"
            :style="{ margin: '20px 10px' }"
            @click="addCart"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsDetail :goods="goodsInfo"></GoodsDetail>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1" />
          <GoodsHot :type="2" />
          <GoodsHot :type="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
